Python Programming with Raspberry Pi Zero by Yamanoor Sai & Yamanoor Srihari

Python Programming with Raspberry Pi Zero by Yamanoor Sai & Yamanoor Srihari

Author:Yamanoor, Sai & Yamanoor, Srihari [Yamanoor, Sai]
Language: eng
Format: azw3
Tags: COM041000 - COMPUTERS / Microprocessors, COM051360 - COMPUTERS / Programming Languages / Python, COM067000 - COMPUTERS / Hardware / General
Publisher: Packt Publishing
Published: 2017-05-04T04:00:00+00:00


Challenge to the reader

The examples discussed with the glob module are available for download along with this chapter as glob_example.py. In one of the examples, we discussed listing files of a specific format. How would you go about listing files that are of the following format: filexxxx.*? (Here x represents any number between 0 and 9. * represents any file extension.)

The shutil module

The shutil module (https://docs.python.org/3/library/shutil.html) enables moving and copying files between folders using the move() and copy() methods. In the previous section, we listed all text files within the folder, txt_files. Let's move these files to the current directory (where the code is being executed) using move(), make a copy of these files once again in txt_files and finally remove the text files from the current directory:

import glob

import shutil

import os

if __name__ == "__main__":

# move files to the current directory

for file in glob.glob('txt_files/file1[0-9][0-9].txt'):

shutil.move(file, '.')

# make a copy of files in the folder 'txt_files' and delete them

for file in glob.glob('file1[0-9][0-9].txt'):

shutil.copy(file, 'txt_files')

os.remove(file)

In the preceding example (available for download along with this chapter as shutil_example.py), the files are being moved as well as copied from the origin to the destination by specifying the source and the destination as the first and second arguments respectively.

The files to be moved (or copied) are identified using the glob module. Then, each file is moved or copied using their corresponding methods.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(149512)
Eco-friendly approach of bio-indigo synthesis and developing purification methods towards isolation of indigo from indirubin and bacterial fragments by Ramalingam Manivannan & Kaliyan Prabakaran & Young-A Son(148228)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74269)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(66072)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(65822)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(65257)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50857)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(31861)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(31810)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(31569)
What's Done in Darkness by Kayla Perrin(27101)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26482)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26435)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(21010)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20773)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20647)
The Fifty Shades Trilogy & Grey by E L James(19604)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19486)
Shot Through the Heart by Mercy Celeste(19345)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17490)